// pages/course_center/course_center.js
const APP = getApp();
const Common = require('../../common/common.js');
const Api = require('../../common/api.js');
const NUM = 10;
var PAGE = 1;
var CanLoadCourseData = true;//是否可以加载数据
Page({
  data: {
    courseParams: {
    },
    courseData: [
      // {
      //   "id": "562",
      //   "name": "VIP助考视频",//名称
      //   "description": "简介",//简介
      //   "weike_nr": "2",//微课数量
      //   "visitor_nr": "10",//观看人数
      //   "face": "http://eagle.snslearn.com/md/e864932e35965dfafd59c99d61ff8c79",//封面图
      // }
    ],
    deviceInfo: null
  },

  onLoad: function (options) {
    let _this = this,
      deviceInfo = wx.getSystemInfoSync();
      _this.setData({
        deviceInfo: deviceInfo
      });

    APP.login(_this);
  },
  /**
   * 获取专辑列表数据
   */
  loadCourseData: function () {
    let _this = this; 
    if (!CanLoadCourseData) {
      return;
    }
    CanLoadCourseData = false;
    wx.showLoading({
      title: '加载中...',
    });
    Common.ajaxGet(APP.globalData.URL + Api.WeixinAppAlbumList, {
      token: wx.getStorageSync('token'),
      page: PAGE,
      num: NUM
    }, function (res) {
      wx.hideLoading();
      if (res.error_code == 0) {
        if( res.data.length < NUM ) {
          CanLoadCourseData = false;
        }
        else {
          CanLoadCourseData = true;
          PAGE++;
        }
        _this.setData({
          courseData: res.data
        });
      }
      else {
        CanLoadCourseData = false;
        wx.showToast({
          title: res.error_msg,
          icon: 'none',
          duration: 1500
        });
      }
    }, function(res){
      wx.hideLoading();
      CanLoadCourseData = true;
      wx.showToast({
        title: '数据请求失败',
        icon: 'none',
        duration: 1500
      });
    });
  },
  navToPage: function(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    });
  },

  onPullDownRefresh: function () {
    let _this = this;
    PAGE = 1;
    CanLoadCourseData = true;
    _this.loadCourseData();
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
    let _this = this;
    _this.loadCourseData();
  }
})